import scrapy
from Hue.basepro import ZhengFuBaseSpider


class YuxiSpider(ZhengFuBaseSpider):
    name = 'Yuxi-pro'
    allowed_domains = ['yuxi.gov.cn']
    start_urls = ['http://http://www.yuxi.gov.cn//']
    api = 'http://www.yuxi.gov.cn/yxgovfront/search_{page}.jspx?q={keyword}&orderBy=time&_s_=1&dir=desc'
    keywords = ['玉溪好人']
    method = 'GET'


    def edit_page(self, response):
        all_result = response.xpath('//div[@class="col col1 search-list"]/div[1]/span[2]/text()').re('\d*')[0]
        all_page = int(all_result) // 10 + 1
        return all_page

    def edit_items_box(self, response):
        items_box = response.xpath('//dl[@class="list3"]')
        return items_box

    def edit_item(self, item):
        meta_info = {
            "title": "".join(item.css('dt > a *::text').getall()).strip(),
            "url": item.xpath('//dd[3]/a/@href').get(),
            "date": item.xpath('//dd[3]/span/text()').get()
        }
        return meta_info
